package com.and9.mchen.mcmusic.dao.impl;

import java.sql.Timestamp;
import java.text.MessageFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.and9.mchen.mcmusic.dao.IBaseEntityDao;
import com.and9.mchen.mcmusic.dao.ILyricDao;
import com.and9.mchen.mcmusic.entity.Lyric;
import com.and9.mchen.mcmusic.exception.DBException;
import com.and9.mchen.mcmusic.exception.DaoException;
import com.and9.mchen.mcmusic.util.EntityHelper;
import com.and9.mchen.mcmusic.util.GEntityHelper;
import com.and9.mchen.mcmusic.util.QueryHelper;

public class LyricDaoImpl implements ILyricDao{

	private final static Log log = LogFactory.getLog(ImageUrlDaoImpl.class);
	
	public static final String TABLEMANE="lyric_table";
	
	public int createOrUpdateLyric(Lyric lyric, String... propertyNames)
			throws DaoException {
		
		if(GEntityHelper.isEmpty(lyric)){
			return -1;
		}
		
		String sqlStr=null;
		Object[] propertyValues=null;
		
		if(lyric.getId() == null || lyric.getId() <= 0){
			
			if(GEntityHelper.isEmpty(lyric.getUniqueId())){
				lyric.setUniqueId(EntityHelper.createUniqueId());
			}
			lyric.setCreateTime(new Timestamp(System.currentTimeMillis()));
			Object[] parentParams=QueryHelper.getFieldsVal(lyric.getParent(), IBaseEntityDao.BASEPROPERTYNAMES);
			Object[] lyricParams=QueryHelper.getFieldsVal(lyric, ILyricDao.BASEPROPERTYNAMES);
			propertyValues=EntityHelper.groupArrays(parentParams, lyricParams);

			propertyNames=EntityHelper.groupArrays(IBaseEntityDao.BASEPROPERTYNAMES, ILyricDao.BASEPROPERTYNAMES);
			sqlStr=QueryHelper.getInsertSQLByProperty(LyricDaoImpl.TABLEMANE, propertyNames);
			
		}else{
			
			if(lyric.getId().equals(1)){
				return 0;
			}
			
			if(GEntityHelper.isEmpty(propertyNames) || 
					EntityHelper.containBaseEntityPropertyName(propertyNames) == true){
				propertyNames=ILyricDao.BASEPROPERTYNAMES;
			}
			propertyValues=QueryHelper.getFieldsVal(lyric, propertyNames);
			sqlStr=QueryHelper.getUpdateSQL(LyricDaoImpl.TABLEMANE, lyric,"id="+lyric.getId(), propertyNames);
		}
		
		try {
			
			return QueryHelper.update(sqlStr, propertyValues);
		} catch (DBException e) {
			log.error("[function: createOrUpdateLyric]", e);
			throw new DaoException("create or update lyric faliure");
		}
	}


	public Lyric getLyricByLyricId(long id) throws DaoException {

		if(id <= 0){
			return null;
		}
		
		String sqlStr=QueryHelper.getSelectSQL(LyricDaoImpl.TABLEMANE, "id=?");
		
		try {
			return QueryHelper.read(Lyric.class, sqlStr, id);
		} catch (DBException e) {
			log.error("[function: getLyricByLyricId]", e);
			throw new DaoException("get lyric by lyric id faliure");
		}
		
	}

	public Lyric getLyricByLyricUniqueId(String uniqueId) throws DaoException {

		if(GEntityHelper.isEmpty(uniqueId)){
			return null;
		}
		
		String sqlStr=QueryHelper.getSelectSQL(LyricDaoImpl.TABLEMANE, "uniqueId=?");

		try {
			return QueryHelper.read(Lyric.class, sqlStr, uniqueId);
		} catch (DBException e) {
			log.error("[function: getLyricByLyricUniqueId]", e);
			throw new DaoException("get lyric by lyric unique id faliure");
		}
	}

	public int deleteLyricByLyricId(long id) throws DaoException {

		if(id <= 0){
			return -1;
		}
		
		if(id == 1){
			return 0;
		}
		
		String sqlStr=QueryHelper.getDeleteSQL(LyricDaoImpl.TABLEMANE, "id=?");
		
		try {
			return QueryHelper.update(sqlStr, id);
		} catch (DBException e) {
			log.error("[function: deleteLyricByLyricId]", e);
			throw new DaoException("delete lyric by lyric id faliure");
		}
	
	}


	public Lyric getLyricBySongId(long id) throws DaoException {
		
		if(id <= 0){
			return null;
		}
		
		String sqlStr=MessageFormat.format("SELECT DISTINCT {0}.* FROM {0}, {1} WHERE " +
				"{0}.id={1}.lyric_id AND {1}.id=? LIMIT 1", LyricDaoImpl.TABLEMANE, 
					SongDaoImpl.TABLEMANE);
		
		try {
			return QueryHelper.read(Lyric.class, sqlStr, id);
		} catch (DBException e) {
			log.error("[function: getLyricBySongId]", e);
			throw new DaoException("get lyric by song id faliure");
		}
		
	}

}
